<?php

/**
 * @author Grover Campos
 * @version 1.0
 * @created 03-ago-2009 06:43:07 p.m.
 */
class Gtux_Model_User extends Gtux_Model_Abstract 
{
  /**
   * @var int
   */
  private $_id_user;
  /**
   * @var string
   */
  private $_nombre;
  /**
   * @var string
   */
  private $_apellido;
  /**
   * @var string
   */
  private $_username;
  /**
   * @var sting
   */
  private $_password;
  /**
   * @var Zend_Date
   */
  private $_created_at;
    
  /**
   * Devuelve el id del usuario
   *
   * @return int
   */
  public function getIdUser()
  {
    return $this->_id_user;
  }
  
  /**
   * @param $name
   * @param $value
   */
  public function __set ($name, $value)
  {
    if ($name == 'created_at') 
    {
      if (is_string($value))
      {
        $created_at = new Zend_Date($value, Zend_Date::ISO_8601);
      }
      elseif ($value instanceof Zend_Date) 
      {
        $created_at = $value;
      }
      $this->setCreatedAt($created_at);
    }
    else 
    {
      parent::__set($name, $value);
    }
  }

  /**
   * Establece el id del usuario
   * 
   * @param int $id_user
   * @return Gtux_Model_User
   */
  public function setIdUser($id_user)
  {
    if (Zend_Validate::is($id_user, 'Int'))
    {
      $this->_id_user = $id_user;
    }
    return $this;
  }

  /**
   * Devuelve el nombre del usuario
   *
   * @return string
   */
  public function getNombre()
  {
    return $this->_nombre;
  }

  /**
   * Establece el nombre
   * 
   * @param string $nombre
   * @return Gtux_Model_User
   */
  public function setNombre($nombre)
  {
    $this->_nombre = trim($nombre);
    return $this;
  }

  /**
   * Devuelve el apellido
   *
   * @return string
   */
  public function getApellido()
  {
    return $this->_apellido;
  }

  /**
   * Establece el apellido
   * 
   * @param string $apellido
   * @return Gtux_Model_User
   */
  public function setApellido($apellido)
  {
    $this->_apellido = trim($apellido);
    return $this;
  }

  /**
   * Devuelve el username
   *
   * @return string
   */
  public function getUsername()
  {
    return $this->_username;
  }

  /**
   * Establece el username
   * 
   * @param string $username
   * @return Gtux_Model_User
   */
  public function setUsername($username)
  {
    $this->_username = trim($username);
    return $this;
  }

  /**
   * Devuelve el password
   *
   * @return string
   */
  public function getPassword()
  {
    return $this->_password;
  }

  /**
   * Establece el password
   * 
   * @param string $password
   * @return Gtux_Model_User
   */
  public function setPassword($password)
  {
    $this->_password = trim($password);
    return $this;
  }
  
  /**
   * @param Zend_Date $_created_at to set
   */
  public function setCreatedAt (Zend_Date $_created_at)
  {
    $this->_created_at = $_created_at;
  }

  /**
   * @return Zend_Date 
   */
  public function getCreatedAt ()
  {
    if (null == $this->_created_at) 
    {
      $this->_created_at = Zend_Date::now(Zend_Registry::get('locale'));
    }
    return $this->_created_at;
  }

  /**
   * @see Gtux_Model_Abstract::getGateway()
   *
   * @return Gtux_Model_Gateway_Abstract
   */
  public function getGateway()
  {
    if (null == $this->_gateway) 
    {
      $this->setGateway(new Gtux_Model_Gateway_User());
    }
    return $this->_gateway;
  }
  
  /**
   * 
   * @return array
   */
  public function __sleep() 
  {
  	return array('_id_user', '_nombre', '_apellido', '_username', '_password');
  }
  
  /**
   * Proxy para el gateway que devuelve un objeto User a partir de su username
   * @return Gtux_Model_User
   */
  public function findByUsername() 
  {
    return $this->getGateway()->findByUsername($this->getUsername(), $this);
  }

}
?>